﻿using BotDetector.Core.Model;
using BotDetector.Core.Search;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using BotDetector.WebApplication.Models;

namespace BotDetector.WebApplication.Controllers
{
    public class HomeController : Controller
    {
        Search s;
        public ActionResult Index()
        {
            InitializeSession();
            InitializeList();
            return View(s);
        }

        private void InitializeList()
        {
            Phrase phrase = new Phrase()
            {
                stringContentThatWasSearched = "",
                BingGoogleIsBotResult = "",
                BingIsBotResult = "",
                GoogleIsBotResult = ""
            };

            List<Phrase> ls = new List<Phrase>();
            ls.Add(phrase);
            s = new Search()
            {
                Searches = ls
            };
        }

        private void InitializeSession()
        {
            Session["phrase"] = "";
            Session["bingResult"] = "";
            Session["googleResult"] = "";
            Session["bingGoogleResult"] = "";
        }

        [HttpPost]
        public ActionResult GetValue(FormCollection collection)
        {
            string[] phrasee;
            string[] bingResultt;
            string[] googleResultt;
            string[] bingGoogleResult;
            String isBotGoogle = "", isBotBing = "", isBotAggregator = "";
            bool isBot;
            ResultItemList result, resultAggregator;
            string textboxValue = Request.Form["txtOne"];
            List<Phrase> ls = new List<Phrase>();
            String googleName = collection["googlename"];
            String bingName = collection["bingname"];
            BotDetector.Core.Detector.BotDetector botDetector = new BotDetector.Core.Detector.BotDetector();

            if (!string.IsNullOrEmpty(googleName) && string.IsNullOrEmpty(bingName))
            {
                GoogleSearch googleSearch = new GoogleSearch();
                isBotGoogle = SearchOnGoogle(isBotGoogle, textboxValue, botDetector, googleSearch, out isBot, out result);
                ViewBag.Message = isBot ? "Bot" : "NotBot";
            }

            if (!string.IsNullOrEmpty(bingName) && string.IsNullOrEmpty(googleName))
            {
                BingSearch bingSearch = new BingSearch();
                isBotBing = SearchOnBing(isBotBing, textboxValue, botDetector, bingSearch, out isBot, out result);
                ViewBag.Message = isBot ? "Bot" : "NotBot";
            }

            if (!string.IsNullOrEmpty(bingName) && !string.IsNullOrEmpty(googleName))
            {
                isBotAggregator = SearchAggregator(isBotAggregator, textboxValue, out isBot, out resultAggregator);
                ViewBag.Message = isBot ? "Bot" : "NotBot";
            }

            SaveInSession(isBotGoogle, isBotBing, isBotAggregator, textboxValue);
            if (!Session["phrase"].Equals(""))
            {
                SplitFromSession(out phrasee, out bingResultt, out googleResultt, out bingGoogleResult);
                SaveInList(phrasee, bingResultt, googleResultt, bingGoogleResult, ls);
            }

            return View("Index", s);
        }

        private void SaveInList(string[] phrasee, string[] bingResultt, string[] googleResultt, string[] bingGoogleResult, List<Phrase> ls)
        {
            for (int i = 0; i < phrasee.Length - 1; i++)
            {
                Phrase phrase1 = new Phrase()
                {
                    stringContentThatWasSearched = phrasee[i],
                    BingGoogleIsBotResult = bingGoogleResult[i],
                    BingIsBotResult = bingResultt[i],
                    GoogleIsBotResult = googleResultt[i]
                };
                ls.Add(phrase1);
            }
            s = new Search();
            s.Searches = ls;
        }

        private void SplitFromSession(out string[] phrasee, out string[] bingResultt, out string[] googleResultt, out string[] bingGoogleResult)
        {
            string x = Session["phrase"].ToString();
            string y = Session["bingResult"].ToString();
            string z = Session["googleResult"].ToString();
            string v = Session["bingGoogleResult"].ToString();
            phrasee = x.Split('#');
            bingResultt = y.Split('#');
            googleResultt = z.Split('#');
            bingGoogleResult = v.Split('#');
        }

        private void SaveInSession(String isBotGoogle, String isBotBing, String isBotAggregator, string textboxValue)
        {
            Session["phrase"] = Session["phrase"] + textboxValue + "#";
            Session["bingResult"] = Session["bingResult"] + isBotBing + "#";
            Session["googleResult"] = Session["googleResult"] + isBotGoogle + "#";
            Session["bingGoogleResult"] = Session["bingGoogleResult"] + isBotAggregator + "#";
        }

        private static string SearchAggregator(String isBotAggregator, string textboxValue, out bool isBot, out ResultItemList resultAggregator)
        {
            ResultItemList resultGoogle = new ResultItemList();
            ResultItemList resultBing = new ResultItemList();
            BotDetector.Core.Detector.BotDetector bt = new BotDetector.Core.Detector.BotDetector();
            GoogleSearch googleSearch = new GoogleSearch();
            resultGoogle = googleSearch.Search(textboxValue);
            resultGoogle.Phrase = textboxValue;
            BingSearch bingSearch = new BingSearch();
            resultBing = bingSearch.Search(textboxValue);
            resultBing.Phrase = textboxValue;
            GoogleBingAggregator aggregator = new GoogleBingAggregator();
            resultAggregator = aggregator.AggregateGoogleAndBingResultsItemLists(resultGoogle, resultBing);
            isBot = bt.IsBot(resultAggregator);
            isBotAggregator = (isBot) ? "Is bot!" : "Not bot!";
            return isBotAggregator;
        }
        private static string SearchOnBing(String isBotBing, string textboxValue, BotDetector.Core.Detector.BotDetector botDetector, BingSearch bingSearch, out bool isBot, out ResultItemList result)
        {
            result = bingSearch.Search(textboxValue);
            result.Phrase = textboxValue;
            isBot = botDetector.IsBot(result);
            isBotBing = (isBot) ? "Is bot!" : "Not bot!";
            return isBotBing;
        }

        private static string SearchOnGoogle(String isBotGoogle, string textboxValue, BotDetector.Core.Detector.BotDetector botDetector, GoogleSearch googleSearch, out bool isBot, out ResultItemList result)
        {
            result = googleSearch.Search(textboxValue);
            result.Phrase = textboxValue;
            isBot = botDetector.IsBot(result);
            isBotGoogle = (isBot) ? "Is bot!" : "Not bot!";
            return isBotGoogle;
        }
    }
}